package com.meisiabc.app.web.controller;

import com.meisiabc.app.service.LessonService;
import com.meisiabc.app.web.auth.AssertAuth;
import com.meisiabc.app.web.auth.SecurityContextHolder;
import com.meisiabc.support.Result;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;

import static com.meisiabc.support.Result.OK_CODE;

@RestController
@RequestMapping("/lesson")
public class LessonController {
    @Autowired
    private LessonService lessonService;

    @AssertAuth
    @GetMapping("/list")
    public Result getLessonList() {
        String mobile = SecurityContextHolder.getContext().getMobile();
        return Result.ok(lessonService.getLessonList(mobile));
    }

    @AssertAuth
    @GetMapping("/history")
    public Result getLessonHistory(String lessonId, String type) {
        String mobile = SecurityContextHolder.getContext().getMobile();
        return Result.ok(lessonService.getLessonHistory(mobile, lessonId, type));
    }

    @AssertAuth
    @GetMapping("/report")
    public Result report(String lessonId, String type) {
        String mobile = SecurityContextHolder.getContext().getMobile();
        return Result.ok(lessonService.report(mobile, lessonId, type));
    }

    @AssertAuth
    @GetMapping("/calendar")
    public Result getLessonCalendar(String lessonId, Integer year, Integer monthOfYear) {
        String mobile = SecurityContextHolder.getContext().getMobile();
        return Result.ok(lessonService.getLessonCalendar(mobile, lessonId, LocalDate.of(year, monthOfYear, 1)));
    }

    @AssertAuth
    @GetMapping("/leave")
    public Result leave(@RequestParam Integer scheduleId) {
//        int schedules = json.getInteger("schedule_id");
        String mobile = SecurityContextHolder.getContext().getMobile();
        lessonService.leave(mobile, scheduleId, DateTime.now());
        return new Result(OK_CODE, "请假成功");
    }

    @GetMapping("/count")
    public Result count(String material) {
        return Result.ok(lessonService.count(material));
    }
}
